<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

bigtitle($txt['bnt_warp_title']);

//-------------------------------------------------------------------------------------------------

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Pull sector info into $sectorinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Run checks to ensure player enough turns and warp edit devices
if ($playerinfo['turns'] < 1)
{
	echo $txt['bnt_warp_turn'], '<br><br>';
	TextMainMenu();
	return;
}

if ($playerinfo['dev_warpedit'] < 1)
{
	echo $txt['bnt_warp_none'], '<br><br>';
	TextMainMenu();
	return;
}

$result = $smcFunc['db_query']('', '
	SELECT allow_warpedit
	FROM {raw:db_zones}
	WHERE zone_id = {int:zone_id}',
	array(
		'db_zones' => $dbtables['zones'],
		'zone_id' => $sectorinfo['zone_id']
	)
);
$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['allow_warpedit'] == 'N')
{
	echo $txt['bnt_warp_forbid'], '<br><br>';
	TextMainMenu();
	return;
}

if ($zoneinfo['allow_warpedit'] == 'L')
{
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_zones}
		WHERE zone_id = {int:zone_id}',
		array(
			'db_zones' => $dbtables['zones'],
			'zone_id' => $sectorinfo['zone_id']
		)
	);
	$zoneowner_info = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$result = $smcFunc['db_query']('', '
		SELECT team
		FROM {raw:db_ships}
		WHERE ship_id = {int:owner}',
		array(
			'db_ships' => $dbtables['ships'],
			'owner' => $zoneowner_info['owner']
		)
	);
	$zoneteam = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);;

	if ($zoneowner_info['owner'] != $playerinfo['ship_id'])
	{
		if (($zoneteam['team'] != $playerinfo['team']) || ($playerinfo['team'] == 0))
		{
			echo $txt['bnt_warp_forbid'], '<br><br>';
			TextMainMenu();
			return;
		}
	}
}

$links_exist = TRUE;

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_links}
	WHERE link_start = {int:link_start}
	ORDER BY link_dest ASC',
	array(
		'db_links' => $dbtables['links'],
		'link_start' => $playerinfo['sector']
	)
);

if ($smcFunc['db_num_rows']($result) == 0)
{
	echo $txt['bnt_warp_nolink'], '<br><br>';
	$links_exist = FALSE;
}
else
{
	echo $txt['bnt_warp_linkto'], ' ';
	while($row = $smcFunc['db_fetch_assoc']($result))
	{
		echo '<b>', $row['link_dest'], '</b> ';
	}
	echo '<br><br>';
}

echo '	
		<form action="', $scripturl, '?action=blacknova;sa=warpedit2" method="post">
		<table>
			<tr>
				<td>', $txt['bnt_warp_query'], '</td>
				<td><input type="text" name="target_sector" size="6" maxlength="6" value=""></td>
			</tr>
			<tr>
				<td>', $txt['bnt_warp_oneway'], '</td>
				<td><input type="checkbox" style="check" name="oneway" value="oneway"></td>
			</tr>
		</table>
		<input type="submit" value="', $txt['bnt_submit'], '"><input type="reset" value="', $txt['bnt_reset'], '">
		</form>';
		
if ($links_exist)
{
	echo '
		<br><br>', $txt['bnt_warp_dest'], '<br><br>
		<form action="', $scripturl, '?action=blacknova;sa=warpedit3" method="post">
		<table>
			<tr>
				<td>', $txt['bnt_warp_destquery'], '</td>
				<td><input type="text" name="target_sector" size="6" maxlength="6" value=""></td>
			</tr>
			<tr>
				<td>', $txt['bnt_warp_bothway'], '</td>
				<td><input type="checkbox" style="check" name="bothway" value="bothway"></td>
			</tr>
		</table>
		<input type="submit" value="', $txt['bnt_submit'], '"><input type="reset" value="', $txt['bnt_reset'], '"><br><br>
		</form>';
}

TextMainMenu();
?>
